﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using NPMonitor.Implementation;
using NPMonitor.DataModel.Users;

namespace NPMonitor.Web.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            rfvUsername.ErrorMessage = string.Format(ValidationString.FieldRequired, "Username");
            rfvPassword.ErrorMessage = string.Format(ValidationString.FieldRequired, "Password");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var fullUsername = txtUsername.Text.Trim();
            string userDomain = "";
            string username = "";
            string password = "";
            if (fullUsername.Contains('\\'))
            {
                var arrayuser = fullUsername.Split('\\');
                if (arrayuser.Length == 2)
                {
                    userDomain = arrayuser[0];
                    username = arrayuser[1];
                    password = txtPassword.Text;
                    ValidateUserInfo(userDomain, username, password);
                }
                else
                {
                    lblErroeMsg.Text = ValidationString.LoginFailure;
                }
            }
            else
            {
                username = fullUsername;
                password = txtPassword.Text;
                ValidateUserInfo(userDomain, username, password);
            }


        }

        private void ValidateUserInfo(string userDomain, string username, string password)
        {
            var accountValidation = AccountValidation.GetInstance();
            if (accountValidation.ImpersonateValidUser(username, userDomain, password))
            {
                Users user = StoreManager.Current.UserStore.GetUserInfoByAccount(username);
                try
                {
                    if (user != null && user.Status == UserStatus.Active.ToString())
                    {
                        bool remeberChecked = chkRemeber.Checked;
                        string userData = string.Format("{0}|{1}", user.UserId, username);
                        FormsAuthentication.SetAuthCookie(userData, remeberChecked);
                        Response.Redirect(FormsAuthentication.GetRedirectUrl(username, true));
                    }
                    else if (user != null)
                    {
                        lblErroeMsg.Text = ValidationString.LoginUserDisabled;
                    }
                    else
                    {
                        lblErroeMsg.Text = ValidationString.LoginFailure;
                    }

                }
                catch (Exception ex)
                {
                    lblErroeMsg.Text = ex.Message;
                }


            }
            else
            {
                lblErroeMsg.Text = ValidationString.LoginFailure;
            }
        }
    }   
}    
